const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: "",
    info: "",
    userInfo: ''
  },

  onLoad() {
    this.getAuthentic();
  },


  //获取用户信息 主要是拿头像
  getUserInfo() {
    var that = this;
    app.ajax({
      url: 'Partner/User/getPartnerInfo',
      data: {},
      success: function (res) {
        if (res.data.code == 200) {
          that.setData({
            userInfo: res.data.data,
          })
        }
      },
    })
  },


  //用*格式化字符串
  formatStarStr(str, frontLen, endLen) {
    let len = str.length - frontLen - endLen;
    let xing = '';
    for (let i = 0; i < len; i++) {
      xing += '*';
    }
    return str.substring(0, frontLen) + xing + str.substring(str.length - endLen);
  },

  //获取认证信息
  getAuthentic() {
    app.ajax({
      url: "Partner/Setup/get_authentication",
      success: res => {
        if (res.statusCode == 200) {
          if (res.data.code == 1000) {
            if (res.data.data) {
              const info = res.data.data;
              if (info.type == 1) {
                this.getUserInfo();
                info.id_card = info.personal_ID_card ? this.formatStarStr(info.personal_ID_card, 6, 2) : "";
              } else {
                info.credit_code = info.company_number ? this.formatStarStr(info.company_number, 6, 4) : "";
              }
              this.setData({
                type: parseInt(info.type),
                info
              })
            } else {
              this.setData({
                type: 0
              })
            }
          } else {
            app.showToast(res.data.message);
          }
        } else {
          app.showToast("网络错误！请稍后再试~");
        }
      }
    })
  },


  //跳转到填写信息界面
  navAuthentic(e) {
    const {
      type
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '../authentic/authentic?type=' + type
    })
  },

  navDetail(e) {
    const type = this.data.type;
    const mold = e.currentTarget.dataset.mold;
    wx.navigateTo({
      url: '../detail/detail?type=' + type + "&mold=" + mold,
      success: res => {
        res.eventChannel.emit('acceptDataFromOpenerPage', this.data.info)
      }
    })
  },

  edit(e) {
    const type = this.data.type == 1 ? 1 : 2;
    wx.navigateTo({
      url: '../authentic/authentic?id=' + this.data.info.id + "&type=" + type,
      success: res => {
        res.eventChannel.emit('acceptDataFromOpenerPage', this.data.info)
      }
    })
  }

})